﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.SharePoint;
using Microsoft.SharePoint.PowerShell;

using Microsoft.Office.Server.Search.Administration;
using Microsoft.Office.Server.Search.Cmdlet;

namespace Community.SharePoint.PowerShell
{
    /// <summary>
    /// SharePoint PowerShell Community Kit
    /// New-SPEnterpriseSearchQueryScopeDisplayGroup
    /// This cmdlet creates a new scope display group given a site collection
    /// URL and SearchServiceApplicationPipeBind
    /// Future Commands: Set-SPEnterpriseSearchQueryScopeDisplayGroupDefault
    ///                  Remove-SPEnterpriseSearchQueryScopeDisplayGroup
    ///                  Get-SPEnterpriseSearchQueryScopeDisplayGroup
    /// Example (Creates a new scope display group called "New Display Group"): 
    /// $searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
    /// $group = Get-SPEnterpriseSearchQueryScopeDisplayGroup -Name "New Scope Display Group" -url http://sp2010 -SearchApplication $searchapp
    /// Remove-SPEnterpriseSearchQueryScopeDisplayGroup $group
    /// Author: Corey Roth (coreyroth@gmail.com)
    /// Creation Date: 8/9/2010
    /// http://www.dotnetmafia.com
    /// </summary>
    [Cmdlet(VerbsCommon.New, "SPEnterpriseSearchQueryScopeDisplayGroup")]
    public class NewEnterpriseSearchQueryScopeDisplayGroup : SPNewCmdletBase<ScopeDisplayGroup>
    {
        [Parameter(Mandatory = true, Position = 1)]
        public string Name { get; set; }

        [Parameter(Mandatory = false, Position = 3)]
        public string Description { get; set; }

        [Parameter(Mandatory = true), Alias(new string[] { "o" })]
        public Uri Url { get; set; }

        [Parameter(Mandatory = false), DefaultValue(true), Alias(new string[] { "disp" })]
        public bool? DisplayInAdminUI { get; set; }

        [Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0)]
        public SearchServiceApplicationPipeBind SearchApplication { get; set; }

        protected override ScopeDisplayGroup CreateDataObject()
        {
            // set a default description and bool - need to update attributes to take care of this
            if (string.IsNullOrEmpty(Description))
                Description = Name;
            if (!DisplayInAdminUI.HasValue)
                DisplayInAdminUI = true;

            // get the search applicatioin from the pipe bind
            SearchServiceApplication searchServiceApplication = SearchApplication.Read();

            Scopes scopes = new Scopes(searchServiceApplication);
            ScopeDisplayGroupCollection scopeDisplayGroups = scopes.AllDisplayGroups;

            // create the scope display group
            ScopeDisplayGroup scopeDisplayGroup = scopeDisplayGroups.Create(Name, Description, Url, DisplayInAdminUI.Value);

            return scopeDisplayGroup;
        }
    }
}
